package cn.wolfcode.luowowo.website.interceptor;

import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//登录拦截器.
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {

    //    @Autowired
//    private StringRedisTemplate redisTemplate;????
//
    //引入的信息包含redis存取得方法.
    @Reference
    private IUserInfoCacheService userInfoCacheService;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //1.判断是否是动态资源
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            //判断本地session是否有登录,有值,就放行.
            HttpSession session = request.getSession();
            Object userInfo = session.getAttribute(Consts.USER_INFO);
            if (userInfo != null) {
                return true;
            }
            //2.本地没有,查看远程redis,是从cookie中获取到token,不是原本就有的.
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals(Consts.USER_INFO_TOKEN)) {
                        String token = cookie.getValue();
                        String json = userInfoCacheService.getUserInfoCache(token);
                        //转化为UserInfo对象

                            UserInfo info = JSON.parseObject(json, UserInfo.class);
                            //存入本地session中
                            session.setAttribute(Consts.USER_INFO, info);
                            return true;
                    }
                }
            }
            //判断资源是否需要登录访问,不需要,放行. //???位置.
            if(!handlerMethod.hasMethodAnnotation(RequiredLogin.class)){
                return true;
            }

            //判断ajax请求的登录失败问题
            if(handlerMethod.hasMethodAnnotation(ResponseBody.class)){
                AjaxResult result = new AjaxResult();
                result.setCode(AjaxResult.NO_LOGIN_CODE);
                result.mark("请先登录后在操作");
                response.setContentType("text/json;charset=utf-8");
                response.getWriter().print(JSON.toJSONString(result));
            }else{
                response.sendRedirect("/login.html");
            }

            //需要登录,用户没登录,重定向
            return  false;  //拦截

        }


        return true; //静态资源.
    }
}
